<template>
    <div>
        <PageTitle :title="titleName"></PageTitle>
        <div class="form-wrapper">
            <el-form :model="ruleForm" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="活动名称" prop="company">
                    <el-input v-model="ruleForm.activity_name" placeholder="请输入活动名称"></el-input>
                </el-form-item>
                <el-form-item label="领取次数" prop="receive">
                    <el-input-number v-model="ruleForm.receive" :min="1" label="请输入领取次数"></el-input-number>
                </el-form-item>
                <el-form-item label="时间" prop="date">
                    <el-date-picker
                        v-model="ruleForm.date"
                        type="datetimerange"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        :default-time="['12:00:00']">
                    </el-date-picker>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm">提交</el-button>
                    <el-button @click="$router.go(-1)">取消</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    import PageTitle from '@/components/pageTitle/PageTitle.vue'
    export default {
        name: 'FirstTypeDetail',
        components: {
            PageTitle
        },
        activated() {
            this.ruleForm = {
                activity_name:null,//活动名称
                receive:1,//领取次数
                date:"",
                id:null,
            }
            if(this.$route.params.id != 'none') {
                this.ruleForm.company = this.$route.params.company
                this.detail()
            }
           
        },
        deactivated() {
            
            
        },
        data() {
            return {
                ruleForm: {
                    activity_name:null,//活动名称
                    receive:null,//领取次数
                    date:"",
                    id:null,
                },
            }
        },
        computed: {
            titleName() {
                return this.$route.params.id == 'none' ? '添加活动' : '查看活动'
            },
            submitUrl() {
                return this.$route.params.id == 'none' ? this.$api + '/v1/channel/channel' : this.$api + '/v1/channel/modify'
            }
        },
        methods: {
            //获取详情
            async detail(){
                let {data: { code, data}} = await this.$axios({
                    url: this.$api + "/v1/detailed/show",
                    method: "GET",
                    params: {
                        id: this.$route.params.id
                    }
                });
                console.log('活动详情--------',data)
                if (code === 0) {
                    this.ruleForm.activity_name = data.channel_name
                    this.ruleForm.date = [data.start_at,data.end_at]
                    this.ruleForm.receive = data.receive,
                    this.ruleForm.id = data.id
                }
            },
            //提交表单
            async submitForm(){
                if (!this.ruleForm.activity_name) {
                    this.$message({
                        showClose: true,
                        message: '活动名称不正确',
                        type: 'warning'
                    });
                }else if (!this.ruleForm.receive) {
                    this.$message({
                        showClose: true,
                        message: '领取次数不正确',
                        type: 'warning'
                    });
                }else if (this.ruleForm.date.length<1) {
                    this.$message({
                        showClose: true,
                        message: '活动时间不正确',
                        type: 'warning'
                    });
                }else{
                    let {data: { code, data }} = await this.$axios({
                        url: this.submitUrl,
                        method: "POST",
                        data: {
                            channel_name:this.ruleForm.activity_name,
                            receive:this.ruleForm.receive,
                            start_at:this.ruleForm.date[0],
                            end_at:this.ruleForm.date[1],
                            id:this.ruleForm.id
                        }
                    });
                    console.log("活动编辑----------", data);
                    if (code == 0) {
                        this.$message({
                            showClose: true,
                            message: data,
                            type: 'success'
                        });
                        
                        this.$router.push('/activity')
                    }
                }
                
            },

        }
    }
</script>

<style>
</style>
